﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class CardsControl : MonoBehaviour {

    public List<NNCards> cards;
    //各个位置的牌的父物体
    public List<GameObject> cardsFatherObj;
    //庄家 天地玄黄 位置的牌
    public List<int> cardsList1;
    public List<int> cardsList2;
    public List<int> cardsList3;
    public List<int> cardsList4;
    public List<int> cardsList5;
    public List<int> pokerTypeList;

	void Start () {
        //Destroy(gameObject,4f);              
        //初始化牌
        for (int i = 0; i < 5; i++)
        {
            cards[0].cardsSprit[i].gameObject.SetActive(true);
            cards[1].cardsSprit[i].gameObject.SetActive(true);
            cards[2].cardsSprit[i].gameObject.SetActive(true);
            cards[3].cardsSprit[i].gameObject.SetActive(true);
            cards[4].cardsSprit[i].gameObject.SetActive(true);
          
            cards[0].cardsSprit[i].spriteName = "0";
            cards[1].cardsSprit[i].spriteName = "0";
            cards[2].cardsSprit[i].spriteName = "0";
            cards[3].cardsSprit[i].spriteName = "0";
            cards[4].cardsSprit[i].spriteName = "0";
        }

        StartCoroutine(ShowCards());
	}

    /// <summary>
    /// 展示各位置的牌
    /// </summary>
    public void ShowPlayersCards()
    {       
        for (int i = 0; i < 5;i++ )
        {
            cards[0].cardsSprit[i].gameObject.SetActive(false);
            cards[1].cardsSprit[i].gameObject.SetActive(false);
            cards[2].cardsSprit[i].gameObject.SetActive(false);
            cards[3].cardsSprit[i].gameObject.SetActive(false);
            cards[4].cardsSprit[i].gameObject.SetActive(false);
        }

        for (int i = 0; i < 5;i++ )
        {
            cards[0].cardsSprit[i].gameObject.SetActive(true);
            cards[1].cardsSprit[i].gameObject.SetActive(true);
            cards[2].cardsSprit[i].gameObject.SetActive(true);
            cards[3].cardsSprit[i].gameObject.SetActive(true);
            cards[4].cardsSprit[i].gameObject.SetActive(true); 
        }

        for (int i = 0; i < 5;i++ )
        {                                  
            cards[0].cardsSprit[i].spriteName = cardsList1[i].ToString();
            cards[1].cardsSprit[i].spriteName = cardsList2[i].ToString();
            cards[2].cardsSprit[i].spriteName = cardsList3[i].ToString();
            cards[3].cardsSprit[i].spriteName = cardsList4[i].ToString();
            cards[4].cardsSprit[i].spriteName = cardsList5[i].ToString();
             
        }      
    }

    IEnumerator ShowCards()
    {        
        yield return new WaitForSeconds(2f);
        ShowPlayersCards();
        //ShowPokerType(pokerTypeList);
    }

    public void StartShowCards(List<int> cardList1, List<int> cardList2, List<int> cardList3, List<int> cardList4, List<int> cardList5)
    {
        for (int i = 0; i < 5;i++ )
        {
            cardsList1[i] = cardList1[i];
            cardsList2[i] = cardList2[i];
            cardsList3[i] = cardList3[i];
            cardsList4[i] = cardList4[i];
            cardsList5[i] = cardList5[i];
        }

        //cardsList1 = cardList1;
        //cardsList2 = cardList2;
        //cardsList3 = cardList3;
        //cardsList4 = cardList4;
        //cardsList5 = cardList5;       
    }

    /// <summary>
    /// 展示庄家的牌
    /// </summary>
    /// <param name="bankerCards"></param>
    //public void ShowBankerCards(List<int> bankerCards)
    //{
    //    Debug.Log("设置庄家牌" + Time.time);
    //    for (int i = 0; i < bankerCards.Count;i++ )
    //    {
    //        bankerCardsList[i].gameObject.SetActive(false);
    //        bankerCardsList[i].gameObject.SetActive(true);
    //        bankerCardsList[i].spriteName = bankerCards[i].ToString();
            
    //        Debug.Log("庄家:" + bankerCards[i]);
    //    }
    //}

    /// <summary>
    /// 展示牌型
    /// </summary>
    /// <param name="pokerTypes"></param>
    public void ShowPokerType(List<int> pokerTypes)
    {
        for (int i = 0; i < pokerTypes.Count;i++ )
        {
            if(pokerTypes[i] == 13){
                GameObject wuXiaoNiu = NGUITools.AddChild(GameObject.Find("UI Root/CardsPanel"),(GameObject)Resources.Load(GameDN100PrefabPath.WuXiaoNiu));               
                wuXiaoNiu.transform.localPosition = cardsFatherObj[i].transform.localPosition;
            }
            else if (pokerTypes[i] == 12)
            {
                GameObject wuHuaNiu = NGUITools.AddChild(GameObject.Find("UI Root/CardsPanel"), (GameObject)Resources.Load(GameDN100PrefabPath.WuHuaNiu));
                wuHuaNiu.transform.localPosition = cardsFatherObj[i].transform.localPosition;
            }
            else if (pokerTypes[i] == 11)
            {
                GameObject SiZha = NGUITools.AddChild(GameObject.Find("UI Root/CardsPanel"), (GameObject)Resources.Load(GameDN100PrefabPath.SiZha));
                SiZha.transform.localPosition = cardsFatherObj[i].transform.localPosition;
            }
            else if (pokerTypes[i] == 0)
            {
                GameObject noCow = NGUITools.AddChild(GameObject.Find("UI Root/CardsPanel"), (GameObject)Resources.Load(GameDN100PrefabPath.NoCow));
                noCow.transform.localPosition = cardsFatherObj[i].transform.localPosition;
            }
            else
            {
                GameObject haveCow = NGUITools.AddChild(GameObject.Find("UI Root/CardsPanel"), (GameObject)Resources.Load(GameDN100PrefabPath.HaveCow));
                haveCow.transform.localPosition = cardsFatherObj[i].transform.localPosition;
                NNCardType nnCardType = haveCow.GetComponent<NNCardType>();
                if(pokerTypes[i] == 10){
                    nnCardType.cardSprit.spriteName = "niu";
                }
                else
                {
                    nnCardType.cardSprit.spriteName = pokerTypes[i].ToString();
                }
            }
        }
    }

    public void DestroyGameObject()
    {
        Destroy(gameObject);
    }
}
